package com.gs.api.projects.bzapplets.web;

import com.gs.api.projects.bzapplets.entity.*;
import com.gs.api.projects.bzapplets.service.*;
import com.gs.api.config.Constant;
import com.gs.api.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guoyunfei on 2021/7/9 19:25
 * @className ResultDataController
 * @description TODO
 * @Version 1.0
 */
@Api(tags = "小程序首页信息")
@Slf4j
@RestController
@RequestMapping("{appid}/home")
public class ResultDataController {


    @Autowired
    private BzBannerService bzBannerService;

    @Autowired
    private BzClassificationService bzClassificationService;

    @Autowired
    private BzNoticeService bzNoticeService;

    @Autowired
    private BzConfigService bzConfigService;

    @Autowired
    private BzWallpaperService bzWallpaperService;

    @Autowired
    private BzBaseInfoService bzBaseInfoService;


    @ApiOperation(value = "首页信息加载", notes="首页信息加载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("index")
    public Result index(@PathVariable String appid){
        Map<String,Object> map = new HashMap<>(6);
        // TODO 全局配置信息

        List<BzConfig> config = bzConfigService.getConfig(Constant.API_OSS_TYPE);
        String fileUrl = config.get(0).getValue();
        try {
            // TODO banner
            BzBanner banner = new BzBanner();
            banner.setEnable(1);
            banner.setAppid(appid);
            List<BzBanner> bzBannerList = bzBannerService.bannerList(banner);
            List<BzBanner> iconNav = new ArrayList<>();
            List<BzBanner> banners = new ArrayList<>();
            for (int i = 0; i < bzBannerList.size(); i++) {
                String imageUrl = fileUrl + bzBannerList.get(i).getImage();
                bzBannerList.get(i).setImage(imageUrl);
                if(bzBannerList.get(i).getType() == 2){
                    iconNav.add(bzBannerList.get(i));
                }else{
                    banners.add(bzBannerList.get(i));
                }
            }
            map.put("banners",banners);
            map.put("iconNav",iconNav);
            // TODO banner end


            // TODO 分类
            BzClassification classification = new BzClassification();
            classification.setEnable(1);
            //多账号时开放
            classification.setAppid(appid);
            List<BzClassification> classifications = bzClassificationService.selectList(classification);
            map.put("classifications",classifications);

            // TODO 消息公告
            BzNotice notice = new BzNotice();
            notice.setEnable(1);
            List<BzNotice> notices = bzNoticeService.selectList(notice);
            map.put("notices",notices);
            BzBaseInfo info = bzBaseInfoService.selectOne(appid);
            //设置小程序logo
            map.put("logo",info.getAppLogo());
            map.put("title",info.getAppName());

            //首页热门列表
            List<Map<String, Object>> hots = bzWallpaperService.getHots(appid);
            map.put("hot",hots);

            return Result.success(map);
        }catch (Exception e){
            log.error("请求首页信息出错：{}",e.toString());
            return Result.failure("服务器错误");
        }
    }


}
